#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Richard_Kong
"""
员工管理接口API 测试脚本的编写
步骤：
1、导包
2、定义一个类
3、重写前后置脚本
4、写测试用例
"""
import unittest

from config import config
from api import user_manage_api
from common.common_assert import common_assert
import requests

from tools.HTMLTestRunner import HTMLTestRunner


class Test_User_API(unittest.TestCase):
    def setUp(self) -> None:
        self.session = requests.Session()
        self.user_manage_api = user_manage_api.User_Manage_API()
        pass

    def tearDown(self) -> None:
        if self.session:
            self.session.close()
        pass

    def test01_user_add(self):
        response = self.user_manage_api.user_add_api(config.user_add_url, self.session, config.user_add_data)
        print(response.json())
        config.user_id = response.json().get("data").get('id')

        """
        这两个url组合不能放在这个函数中，这样如果出问题了 非常不好找，应该放到接口文件中
        """
        # config.user_modify_url = config.user_modify_url.format(config.user_id)
        # config.user_delete_url = config.user_delete_url.format(config.user_id)

        common_assert(self,response,True,10000,'操作成功！',200)
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(True, response.json().get("success"))
        # self.assertEqual(10000, response.json().get('code'))
        # self.assertEqual('操作成功！', response.json().get('message'))


        pass

    def test02_user_modify(self):
        response = self.user_manage_api.user_modify_api(self.session, config.user_modify_data)
        common_assert(self, response, True, 10000, '操作成功！', 200)
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(True, response.json().get("success"))
        # self.assertEqual(10000, response.json().get('code'))
        # self.assertEqual('操作成功！', response.json().get('message'))
        self.assertEqual(config.user_id, response.json().get('data').get('id'))
        pass
    @unittest.skip
    def test03_user_delete(self):
        response = self.user_manage_api.user_delete_api(self.session)
        common_assert(self, response, True, 10000, '操作成功！', 200)
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(True, response.json().get("success"))
        # self.assertEqual(10000, response.json().get('code'))
        # self.assertEqual('操作成功！', response.json().get('message'))
        self.assertEqual(config.user_id, response.json().get('data').get('id'))
        pass


if __name__ == '__main__':
    test_suite = unittest.TestSuite()
    test_suite.addTest(unittest.makeSuite(Test_User_API))
    file = "./rp.html"
    with open(file=file,mode='wb') as f:
        runner = HTMLTestRunner(stream=file,title="test_report03")
        runner.run(test_suite)